package com.alt.base;

import android.app.TabActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;

import com.alt.R;
import com.alt.manager.ALTAppMgr;
import com.alt.shared.GlobalExceptionHandler;
import com.alt.shared.IServiceCommandResult;
import com.alt.shared.onImageSet;

public class ALTMainForm extends TabActivity implements OnTabChangeListener,
		IServiceCommandResult {

	// Flag if receiver is registered

	// I think this is the broadcast you need for something like an incoming
	// call
	private String NETWORK_STATE_CHANGE = "android.net.conn.CONNECTIVITY_CHANGE";

	public static final String TAG_TAB_HOME = "TAB_HOME";
	public static final String TAG_TAB_PERSONAL = "TAB_PERSONAL";
	public static final String TAG_TAB_EXCHANGE = "TAB_EXCHANGE";
	public static final String TAG_TAB_SETTING = "TAB_SETTING";	

	private final String[] TABS_TAG = { TAG_TAB_HOME, TAG_TAB_PERSONAL,
			TAG_TAB_EXCHANGE, TAG_TAB_SETTING };
	private final int[] TABS_DRAW = { R.drawable.tab_home, R.drawable.tab_cart,
			R.drawable.tab_call, R.drawable.tab_payment };
	private final Class<?>[] TABS_CLASS = { ALTHomeTab.class,
			ALTCartTab.class, ALTCallTab.class, ALTPaymentTab.class
			};

	private ALTAppMgr m_AppMgr = null;
	private TabHost m_TabHost = null;
	private TabWidget m_TabWidget;

	public ALTTabGroup m_HomeGroup;
	public ALTTabGroup m_CartGroup;
	public ALTTabGroup m_CallGroup;
	public ALTTabGroup m_PaymentGroup;

	private boolean m_Loading = true;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.screen_main_form);

		m_AppMgr = ALTAppMgr.getInstance();
		m_AppMgr.setMainForm(this);

		initTabHost();

		// set exception handler
		Thread.setDefaultUncaughtExceptionHandler(new GlobalExceptionHandler(
				this));
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public void onPause() {
		super.onPause();
		// Make sure you unregister your receivers when you pause your activity
	}

	/**
	 * 
	 */
	private void initTabHost() {

		String TempTabName = m_AppMgr.m_SelectTabName;

		m_TabHost = getTabHost();
		m_TabWidget = getTabWidget();

		m_TabHost.setOnTabChangedListener(this);

		for (int index = 0; index < TABS_TAG.length; index++) {
			View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, m_TabWidget, false);
			ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
			icon.setImageResource(TABS_DRAW[index]);

			m_TabHost.addTab(m_TabHost
					.newTabSpec(TABS_TAG[index])
					.setIndicator(tabIndicator)
					.setContent(
					 new Intent(this, TABS_CLASS[index]).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
			
			
			m_TabWidget.getChildAt(0).setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					// if (TabInfo.TabContext.getCurrentTabTag().equals("tab0")) {
					m_TabHost.setCurrentTab(2);
					m_TabHost.setCurrentTab(0);
					// }

				}
			});						
			
			if (index == 1) {
				m_TabWidget.getChildAt(1).setOnClickListener(
						new OnClickListener() {
							public void onClick(View v) {
								m_TabHost.setCurrentTab(2);
								m_TabHost.setCurrentTab(1);
							}
						});
			}

			if (index == 2) {
				m_TabWidget.getChildAt(2).setOnClickListener(
						new OnClickListener() {
							public void onClick(View v) {
								m_TabHost.setCurrentTab(3);
								m_TabHost.setCurrentTab(2);
							}
						});
			}

			if (index == 3) {
				m_TabWidget.getChildAt(3).setOnClickListener(
						new OnClickListener() {
							public void onClick(View v) {
								m_TabHost.setCurrentTab(2);
								m_TabHost.setCurrentTab(3);
							}
						});
			}			
		}	

		// hack to set font size
		LinearLayout ll = (LinearLayout) m_TabHost.getChildAt(0);
		TabWidget tw = (TabWidget) ll.getChildAt(2);

		RelativeLayout rllf;
		int nTwCount = tw.getChildCount();
		for (int i = 0; i < nTwCount; i++) {
			rllf = (RelativeLayout) tw.getChildAt(i);
			rllf.setPadding(0, 0, 0, 0);
			// first tab
			// ImageView Iv = (ImageView) rllf.getChildAt(0);
			// Iv.setVisibility(View.GONE);
			TextView lf = (TextView) rllf.getChildAt(1);
			lf.setTextSize(20);
			lf.setPadding(0, 0, 0, 20);
		}

		m_Loading = false;
		setTabChanged(TempTabName);
	}

	/**
	 * 
	 * @param tabName
	 */
	public void setTabChanged(String tabName) {
		m_TabHost.setCurrentTabByTag(tabName);
	}

	/**
	 * 
	 * @param tabIndex
	 */
	public void setTabChanged(int tabIndex) {
		m_TabHost.setCurrentTab(tabIndex);
	}

	/**
	 * 
	 * @param index
	 * @return
	 */
	public ALTTabGroup getTabGroup(int index) {
		switch (index) {
		case 0:
			return m_HomeGroup;
		case 1:
			return m_CartGroup;
		case 2:
			return m_CallGroup;
		case 3:
			return m_PaymentGroup;
		default:
			return null;
		}
	}

	private void updateTabIndicators(int selectedIndex) {
		int imgRes = 0;
		for (int i = 0; i < 4; i++) {
			imgRes = getTabIndicatorImgRes(i, false);
			changeTabIndicator(i, imgRes);
		}

		imgRes = getTabIndicatorImgRes(selectedIndex, true);
		changeTabIndicator(selectedIndex, imgRes);
	}

	/**
	 * 
	 * @param tabIndex
	 * @param isSelected
	 * @return
	 */
	public int getTabIndicatorImgRes(int tabIndex, boolean isSelected) {
		int imgRes = 0;

		switch (tabIndex) {
		case 0:
			if (isSelected) {
				imgRes = R.drawable.bg_danhsach;
			} else {
				imgRes = R.drawable.bg_danhsach_none;
			}

			break;
		case 1:

			if (isSelected) {
				imgRes = R.drawable.bg_thongtin;
			} else {
				imgRes = R.drawable.bg_thongtin_none;
			}

			break;

		case 2:
			if (isSelected) {
				imgRes = R.drawable.bg_latter_store;
			} else {
				imgRes = R.drawable.bg_latter_store_none;
			}
			break;
		case 3:
			if (isSelected) {
				imgRes = R.drawable.bg_caidat;
			} else {
				imgRes = R.drawable.bg_caidat_none;
			}
			break;
		case 4:

			if (isSelected) {
				imgRes = R.drawable.bg_caidat;
			} else {
				imgRes = R.drawable.bg_caidat_none;
			}

			break;

		default:
			break;
		}
		return imgRes;
	}

	/**
	 * 
	 * @param index
	 * @param drawable
	 */
	public void changeTabIndicator(int index, int drawable) {
		ImageView iv = (ImageView) m_TabWidget.getChildAt(index).findViewById(
				R.id.icon);
		iv.setImageResource(drawable);
	}

	/**
	 * 
	 */
	public void onTabChanged(String tabName) {
		// TODO Auto-generated method stub
		if (m_Loading) {
			return;
		}

		int i = getTabHost().getCurrentTab();

		Log.i("ALTMainForm", "onTabChanged:" + i);
		m_AppMgr.m_SelectTabName = tabName;

		updateTabIndicators(i);
	}


	public void processCommandResult(int commandCode, Object resultData) {
		// TODO Auto-generated method stub		
	}

}